#include "loginstatus.h"
#include "ui_loginstatus.h"
//#include <QDebug>

LoginStatus::LoginStatus( GlobalData *globalDataIn,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginStatus)
{
    globalData=globalDataIn;
    move(0,0);
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);    //无边框
    this->setAttribute(Qt::WA_QuitOnClose, true);

    model = new QStandardItemModel();
    ui->tableView->setModel(model);
    model->setColumnCount(2);
    model->setHeaderData(0,Qt::Horizontal,QString::fromLocal8Bit("IP"));
    model->setHeaderData(1,Qt::Horizontal,QString::fromLocal8Bit("HOST"));
    ui->tableView->setColumnWidth(0,215);
    ui->tableView->setColumnWidth(1,215);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    for(int i = 0;i<20;i++)
    {
        model->setItem(i,0,new QStandardItem(""));
    }
    hide();
}

LoginStatus::~LoginStatus()
{
}

void LoginStatus::foundControllerIpList()
{
    if(controllerIp.size() > 0)
    {
        if(controllerIp[controllerIp.size()-1] == "end")
        {
            ui->label->setText(tr("搜索结束!"));
        }
        else
        {
            ui->label->setText(tr("正在搜索..."));
        }
    }
    else
    {
        ui->label->setText(tr("正在搜索..."));
    }

    ui->scanbutton->setEnabled(true);

//    ui->tableView->setModel(model);
//    model->setColumnCount(2);
//    ui->tableView->setColumnWidth(0,215);
//    ui->tableView->setColumnWidth(1,215);
//    model->setHeaderData(0,Qt::Horizontal,QString::fromLocal8Bit("IP"));
//    model->setHeaderData(1,Qt::Horizontal,QString::fromLocal8Bit("HOST"));

    for(int i = 0;i<20;i++)
    {
        if(i<controllerIp.size())
        {
            if(controllerIp[i] == "end")
            {
                model->item(i,0)->setText("");
            }
            else
            {
                model->item(i,0)->setText(controllerIp[i]);
            }
        }
        else
        {
            model->item(i,0)->setText("");
        }
    }
}

void LoginStatus::changeLanguage()
{
    ui->retranslateUi(this);//mark
}

void LoginStatus::on_scanbutton_clicked()
{
    //qDebug()<<"on_scanbutton_clicked()";

//    globalData->lock();
    globalData->findFlag = true;    //允许开始寻找 ip
//    globalData->unlock();

    emit scanControllerIp();        //开始

    ui->scanbutton->setEnabled(false);
    ui->label->setText(tr("正在扫描可用IP,请稍后..."));
}

void LoginStatus::on_loginbutton_clicked()
{
    //qDebug()<<"on_loginbutton_clicked()";

//    globalData->lock();
    globalData->findFlag = false;   //停止寻找 ip
//    globalData->unlock();

    ui->scanbutton->setEnabled(true);

    emit clearFindTimerSignal();    //清空

    //根据选中的IP:正式登录
    int curRow=ui->tableView->currentIndex().row();
    modessl = ui->tableView->model();
    QModelIndex indextemp = modessl->index(curRow,0);
    QVariant datatemp = modessl->data(indextemp);

    //qDebug()<<datatemp.toString();
    if(datatemp.toString() != "")
    {
        emit loginErrorInformation(datatemp.toString());
    }
}

void LoginStatus::on_quitbutton_clicked()
{
//    globalData->lock();
    globalData->findFlag = false;   //停止寻找 ip
//    globalData->unlock();

    ui->scanbutton->setEnabled(true);

    emit clearFindTimerSignal();    //清空
}
